package com.zhibang.sinocare.common.result;

import lombok.*;

import java.io.Serializable;

@Data
@NoArgsConstructor
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Result <T> implements Serializable {
    private static final long serialVersionUID = 2772379571896323478L;

    //是否成功
    private Boolean success;
    //返回状态码
    private Integer code;
    //返回信息
    private String message;
    //统计条数
    private Long count;
    //返回数据
    private T data;

    //创建ResultBean对象，接受数据
    protected static <T> Result<T> build(T data){
        Result<T> result = new Result<T>();
        if (data !=null)
            result.setData(data);
        return result;
    }

    /**
     * 操作成功
     */
    public static <T> Result<T> ok(ResultCode resultCode,T data){
        Result<T> result=build(data); //调用build方法
        result.setSuccess(true);  //操作成功默认返回true
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());//成功
        return result;
    }
    /**
     * 重载
     */
    public static <T> Result<T> ok(ResultCode resultCode,Long count,T data){
        Result<T> result=ok(resultCode,data);
        result.setCount(count);
        return result;
    }

    /**
     * 操作失败
     */
    public static <T> Result<T> fail(ResultCode resultCode,T data){
        Result<T> result = build(data);
        result.setSuccess(false); //操作失败默认false
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

}
